# coding:utf-8
from django.contrib.auth import authenticate
from token_auth.models import Token
from userinfo.models import UserProfile
from django.contrib.auth.models import User
from django.views.decorators.csrf import csrf_exempt
import json
from django.http import HttpResponse
from token_auth.utils import json_response, token_required
from compere.models import Compere
from hlr_django import settings
# 分页器
from  django.core.paginator import Paginator

@csrf_exempt
def comperes(request):
    # 获取

    if request.method == 'GET':
       return get_comperes(request)


    # 发布
    elif request.method == 'POST':
        return add_compere(request)


    # 更新
    elif request.method == 'PUT':
        pass

    #删除
    elif request.method == 'DELETE':
        pass


    elif request.method == 'OPTIONS':
        return json_response({})
    else:
        return json_response({
                                 'error': 'Invalid Method'
                            }, status=405)




def get_comperes(request):
    dic =  request.REQUEST
    host = request.get_host()

    current_page = dic.get('current_page',None)
    num_per_page = dic.get('num_per_page',None)


    if current_page is None or \
        num_per_page is None:
        return  json_response({'error':'Invaild data'},status=401)

    comperes_list = Compere.objects.all().order_by('-create_time')


    paginator = Paginator(comperes_list,num_per_page)

    if int(current_page) >0 and int(current_page) <= paginator.num_pages:
        comperes_current_page_list = paginator.page(current_page).object_list

        compere_response_list = [];
        for compere in  comperes_current_page_list:
            user = compere.user
            create_time = compere.create_time.strftime("%Y-%m-%d")
            # %Y-%m-%d %H:%I:%S

            user_profile = user.get_profile()
            avatar = 'http://'+host+settings.MEDIA_URL+str(user_profile.avatar)
            compere_dic = {'username':user.username,
                            'nickname':user_profile.nick_name,
                            'phone':compere.phone,
                            'age':user_profile.age,
                            'content':compere.content,
                            'createtime':create_time,
                            'avatar':avatar

                            }
            compere_response_list.append(compere_dic)

        return json_response({'list':compere_response_list,
                        'num_pages':paginator.num_pages,})
    else:
        return json_response({'error':'Invaild current page'},status=404)

@csrf_exempt
@token_required
def add_compere(request):
    dic =  request.REQUEST

    phone = dic.get(u'phone',None)
    content = dic.get(u'content',None)

    if phone is not None and content is not None:
        user = request.token.user
        print user
        compere = Compere(phone=phone,content=content,user=user)
        compere.save()

        return json_response({})
    else:
        return json_response({'error':'Invaild data'},status=400)
